<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.3 The New AWT Event Model</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 15:51:06 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>What's New in Java 1.1</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-4-SECT-3">4.3 The New AWT Event Model</A></h2>

<P CLASS=para>
The Java 1.1 change that will probably affect Java
programmers the most is the new event processing model
adopted for the AWT windowing and graphical user interface
(GUI) toolkit.  If you created applets or GUIs with Java
1.0, you know that it was necessary to subclass GUI
components in order to handle events.  This model worked okay
for simple programs, but proved increasingly awkward as
programs became more complex.  Furthermore, with the
development of the JavaBeans API, the AWT package needed an
event model that would allow AWT GUI components to serve as
beans.  For these reasons, Java 1.1 defines a new model
for dispatching and handling events.

<P CLASS=para>
As explained in <A HREF="ch07_01.htm">Chapter 7, <i>Events</i></A>,
the new event handling model is essentially a "callback"
model.  When you create a GUI component, you tell it what
method or methods it should invoke when a particular event
occurs on it (e.g., when the user clicks on a button or selects
an item from a list).  This model is very easy
to use in C and C++ because those languages allow you to
manipulate method pointers--to specify a callback, all you
need to do is pass a pointer to the appropriate function.
In Java, however, methods are not data and cannot be
manipulated in this way.  Only objects can be passed like this
in Java, so to define a Java callback, you must define a
class that implements some particular interface.  Then, you
can pass an instance of that class to a GUI component as a
way of specifying the callback.  When the desired event
occurs, the GUI component invokes the appropriate method
of the object you have specified.

<P CLASS=para>
As you might imagine, this new event handling model can lead
to the creation of many small helper classes.  (Sometimes
these helper classes are known as "adaptor classes" because
they serve as the interface between the body of an
application and the GUI for that application.  They are the
"adaptors" that allow the GUI to be "plugged in" to the
application.)  This proliferation of small classes could
become quite a burden, were it not for the introduction of
inner classes, which, as noted above, allows this kind of
special-purpose class to be nested and defined exactly where
it is needed within your program.

<P CLASS=para>
Despite the major AWT event-handling changes, Java 1.1 does
retain backwards compatibility with the event-handling model
of Java 1.0.  It is an all-or-nothing type of backwards
compatibility, however--the two models are so different from
each other that it is not really possible to mix them within
the same application.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Inner Classes</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Deprecated Features</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
